#!/bin/bash
#
##############################################################################
#                                                                            #
#                          Pi-Star send CW Tool                              #
#                                                                            #
#     Version 1.0, Code, Design and Development by Andy Taylor (MW0MWZ).     #
#                                                                            #
#             Make it simple to send CW from the CLI on Pi-Star.             #
#                                                                            #
##############################################################################
#
if [ "$(id -u)" != "0" ]; then
  echo -e "You need to be root to run this command...\n"
  exit 1
fi

# Make sure config is present
if [ "$(grep -o 'Remote Control' /etc/mmdvmhost | wc -l)" -eq "0" ]; then
  echo -e "Remote Commands not enabled...\n"
  exit 1
fi

# Setup some variables
cmd=/usr/local/bin/RemoteCommand
enable=$(grep -A1 'Remote Control' /etc/mmdvmhost | tail -n 1 | awk -F "=" '{print $2}')
port=$(grep -A2 'Remote Control' /etc/mmdvmhost | tail -n 1 | awk -F "=" '{print $2}')

# Make sure that remote commands are turned on
if [ "${enable}" -eq "0" ]; then
  echo -e "Remote Commands not enabled...\n"
  exit 1
fi


if [ -z "$1" ]
then
      echo "To send CW from your Pi-Star, simply run the command with"
      echo "the following syntax: pistar-sendcw <message>"
      echo "where <message> is the message you want to transmit in CW."
      echo ""
      exit 0
fi

# Swap "." to " " until MMDVM firmware includes "." support
message=$(echo "$*" | tr "." " ")

(cd /var/log/pi-star/; ${cmd} ${port} cw ${message^^})

exit 0
